﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.ComponentModel;
using System.Collections.Generic;
using Galaktika.BI.Extensibility.Dom;
using System.ComponentModel.DataAnnotations;
using Galaktika.BI.Runtime.Services;

namespace Galaktika.BI.Silverlight.ClientApp.ViewModel
{
    public class DefaultSettingOverride
    {
        //[Bindable(true)]
        public class VariableSettingOverride : INotifyPropertyChanged, IEditableObject
        {
            private BrickVariable m_Variable;
            public VariableSettingOverride(BrickVariable var)
            {
                if (var == null)
                {
                    throw new ArgumentNullException("var");
                }
                m_Variable = var;
                m_DefaultValue = var.DefaultValue;
                m_Description = var.Description;
            }

            [Display(Name = "Caption_Name", ResourceType = typeof(Localization))]
            public string Name
            {
                get
                {
                    return m_Variable.Name;
                }
            }

            [Display(Name = "Caption_CurrentValue", ResourceType = typeof(Localization))]
            public string CurrentValue
            {
                get
                {
                    return m_Variable.CurrentValue;
                }
                //set
                //{
                //    if (m_Variable.CurrentValue != value)
                //    {
                //        m_Variable.CurrentValue = value;
                //        this.OnPropertyChanged("CurrentValue");
                //    }
                //}
            }

            private string m_Description;
            public string Description
            {
                get
                {
                    return m_Description;
                }
                set
                {
                    m_Description = value;
                    this.OnPropertyChanged("Description");
                }
            }

            private string m_DefaultValue;
            [Display(Name = "Caption_DefaultValue", ResourceType = typeof(Localization))]
            public string DefaultValue
            {
                get
                {
                    return m_DefaultValue;
                }
                set
                {
                    if (m_DefaultValue != value)
                    {
                        m_DefaultValue = value;
                        this.OnPropertyChanged("DefaultValue");
                    }
                }
            }

            //[Bindable(false)]
            public bool IsChanged
            {
                get
                {
                    return m_DefaultValue != m_Variable.DefaultValue ||
                        m_Description != m_Variable.Description;
                }
            }

            //[Bindable(false)]
            public BrickVariable Variable
            {
                get
                {
                    return m_Variable;
                }
            }

            public void CommitEdit()
            {
                m_Variable.DefaultValue = m_DefaultValue;
                m_Variable.Description = m_Description;
            }

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string name)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }

            #endregion

            #region IEditableObject Members

            private string m_OldDefValue;
            private string m_OldDesc;
            public void BeginEdit()
            {
                m_OldDefValue = m_DefaultValue;
                m_OldDesc = m_Description;
            }

            public void CancelEdit()
            {
                m_DefaultValue = m_OldDefValue;
                m_Description = m_OldDesc;
            }

            public void EndEdit()
            {
                m_OldDefValue = null;
                m_OldDesc = null;
            }

            #endregion
        }

        private IEnumerable<BrickVariable> m_VarSrc;
        public DefaultSettingOverride(IEnumerable<BrickVariable> vars)
        {
            if (vars == null)
            {
                throw new ArgumentNullException("vars");
            }
            m_VarSrc = vars;
        }

        private IEnumerable<VariableSettingOverride> m_Vars;
        public IEnumerable<VariableSettingOverride> Variables
        {
            get
            {
                if (m_Vars == null)
                {
                    var res = new List<VariableSettingOverride>();
                    foreach (var v in m_VarSrc)
                    {
                        res.Add(new VariableSettingOverride(v));
                    }
                    m_Vars = res;
                }

                return m_Vars;
            }
        }
    }
}
